﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using iShoppingDTO;

namespace iShoppingDAO
{
    public class ChiTietGiaoDichDAO
    {
        public static DataTable LayThongTinDauGiaTheoMaTaiKhoan(string mataikhoan)
        {
            DataTable dt = null;
            try
            {
                DateTime date = DateTime.Now;
                string sql = "select CT.MaChiTietGiaoDich, CT.GiaGiaoDich, CT.ThoiGianGiaoDich, CT.TinhTrang, SP.MaSanPham, SP.TenSanPham, SP.GiaKhoiDiem, SP.GiaHienTai, SP.NgayHetHan, SP.HinhAnh " +
                "from ChiTietGiaoDich CT, SanPham SP where " +
                "CT.MaTaiKhoan = '" + mataikhoan + "' AND " +
                    /*CT.TinhTrang = 'Thành công' AND*/
                "CT.MaSanPham = SP.MaSanPham  AND '" + String.Format("{0:M/d/yyyy HH:mm:ss}",date)+ "' BETWEEN SP.NgayDang AND SP.NgayHetHan";
                dt = DataProvider.ExecuteQuery(sql);
            }
            catch (Exception ex)
            {

            }
            return dt;
        }

        public static DataTable LaySanPhamTheoMaTaiKhoan(string mataikhoan)
        {
            DataTable dt = null;
            try
            {
                string sql = "select DISTINCT MaSanPham " +
                "from ChiTietGiaoDich where " +
                "MaTaiKhoan = '" + mataikhoan + "'";
                dt = DataProvider.ExecuteQuery(sql);
            }
            catch (Exception ex)
            {

            }
            return dt;
        }

        public static DataTable LayThongTinDauGiaTheoSanPham(string masanpham)
        {
            DataTable dt = null;
            try
            {
                string sql = "select TK.TenTaiKhoan, CT.MaChiTietGiaoDich, CT.GiaGiaoDich, CT.ThoiGianGiaoDich, CT.TinhTrang " +
                "from ChiTietGiaoDich CT, TaiKhoan TK " +
                "where CT.MaSanPham = '" + masanpham + "' AND TK.MaTaiKhoan = CT.MaTaiKhoan order by CT.GiaGiaoDich desc";
                dt = DataProvider.ExecuteQuery(sql);
            }
            catch (Exception ex)
            {

            }
            return dt;
        }

        public static DataTable LayThongTinDauGiaTheoSanPhamTaiKhoan(string masanpham, string mataikhoan)
        {
            DataTable dt = null;
            try
            {
                string sql = "select TK.TenTaiKhoan, CT.MaChiTietGiaoDich, CT.GiaGiaoDich, CT.ThoiGianGiaoDich, CT.TinhTrang " +
                "from ChiTietGiaoDich CT, TaiKhoan TK " +
                "where CT.MaSanPham = '" + masanpham + "' AND TK.MaTaiKhoan = CT.MaTaiKhoan AND CT.MaTaiKhoan = '" + mataikhoan+"'";
                dt = DataProvider.ExecuteQuery(sql);
            }
            catch (Exception ex)
            {

            }
            return dt;
        }
        public static List<ChiTietGiaoDichDTO> SelectChiTietSP(string masanpham, string mataikhoan)
        {
            List<ChiTietGiaoDichDTO> dsCTGD = new List<ChiTietGiaoDichDTO>();
            string sql = "select * " + "from ChiTietGiaoDich where MaTaiKhoan='" +  mataikhoan + "' and MaSanPham ='"+ masanpham +"'";
            DataTable  dt = DataProvider.ExecuteQuery(sql);
            
            foreach (DataRow dr in dt.Rows)
            {
                ChiTietGiaoDichDTO ctgd = new ChiTietGiaoDichDTO();
                ctgd.MaSanPham = dr["MaSanPham"].ToString();
                ctgd.MaChiTietGiaoDich = dr["MaChiTietGiaoDich"].ToString();
                ctgd.GiaGiaoDich = int.Parse(dr["GiaGiaoDich"].ToString());
                ctgd.ThoiGianGiaoDich = DateTime.Parse(dr["ThoiGianGiaoDich"].ToString());
                ctgd.TinhTrang = dr["TinhTrang"].ToString();
                dsCTGD.Add(ctgd);
            }
            return dsCTGD;
        }
        public static bool UpdateTrangThai(ChiTietGiaoDichDTO chiTietGiaoDichDto)
        {
            bool res = false;
            try
            {

                string sql = "UPDATE ChiTietGiaoDich SET TinhTrang = @TinhTrang WHERE MaChiTietGiaoDich = @MaChiTietGiaoDich";

                List<SqlParameter> sqlParameters = new List<SqlParameter>();

                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@TinhTrang", SqlDbType.NVarChar);
                par[0].Value = "Đã Xác Nhận";
                sqlParameters.Add(par[0]);

                par[1] = new SqlParameter("@MaChiTietGiaoDich", SqlDbType.NVarChar);
                par[1].Value = chiTietGiaoDichDto.MaChiTietGiaoDich;
                sqlParameters.Add(par[1]);

                res = DataProvider.ExecuteNoneQuery(sql, sqlParameters);

            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }
    }
}
